package com.lbz.controller.backend;

import com.lbz.common.Constant;
import com.lbz.common.ServerResponse;
import com.lbz.entity.User;
import com.lbz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 后台用户管理员接口
 *
 * @Author 林必昭
 * @Date 2019/8/28 17:17
 */

@Controller
@RequestMapping("/manage/user/")
public class UserManageController {


    @Autowired
    private UserService userService;

    /**
     * 后台管理员登录UserManageController
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> serverResponse = userService.login(username, password);
        if (serverResponse.isSuccess()){
            User user = serverResponse.getData();
            if (user.getRole()== Constant.Role.ROLE_ADMIN){
                //说明是管理员
                session.setAttribute(Constant.CURRENT_USER,user);
                return serverResponse;
            }else {
                return ServerResponse.createByErrorMessage("不是管理员，无法登陆！");
            }
        }
        return serverResponse;
    }
}
